﻿app.controller('SafetyPlaceConfigController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;


    $scope.GetParkPageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.ParkPageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetParkPageList',
            data: { "SearchText": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ParkPageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.InitQRCode = function (placeId, roomId, placeName) {
        $scope.PlaceName = placeName;
        setTimeout(function () {
            $('#qrcode').html("")
            var qrcode = new QRCode(document.getElementById("qrcode"), {
                text: '',
                width: 300,
                height: 300
            });
            var result = {
                "placeId": placeId,
                "roomId": roomId,
                "placeName": placeName,
                "notice":"请使用蓝信尚安全应用扫描二维码"
            };
            qrcode.makeCode(JSON.stringify(result));
        }, 500);
    }
    $scope.InitParkQRCode = function (parkId, parkName) {
        $scope.ParkName = parkName;
        setTimeout(function () {
            $('#qrcode').html("")
            var qrcode = new QRCode(document.getElementById("qrcode"), {
                text: '',
                width: 300,
                height: 300
            });
            var result = {
                "parkId": parkId,
                "parkName": parkName,
                "notice": "请使用蓝信尚安全应用扫描二维码"
            };
            qrcode.makeCode(JSON.stringify(result));
        }, 500);
    }
    $scope.InitBuildingQRCode = function (id, name) {
        $scope.BuildingName = name;
        setTimeout(function () {
            $('#qrcode').html("")
            var qrcode = new QRCode(document.getElementById("qrcode"), {
                text: '',
                width: 300,
                height: 300
            });
            var result = {
                "buildingId": id,
                "buildingName": name,
                "notice": "请使用蓝信尚安全应用扫描二维码"
            };
            qrcode.makeCode(JSON.stringify(result));
        }, 500);
    }
    $scope.GetParkList = function () {
        $scope.ParkList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetParkList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ParkList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetPlacePageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.PlacePageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetPlacePageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.PageLoading = false;
                if (ret.Code == 100) {
                    $scope.PlacePageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.AddPark = function (model) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/AddPark',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = "ParkPageList?page=1";
                }, 2000);


            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.DeletePark = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../SafetyConfig/DeletePark',
                    data: { 'Id': Id }
                }).success(function (ret) {
                    $scope.DataLoading = false;
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        alertify.alert(ret.Msg);
                        $scope.GetParkPageList(model, page);

                    } else {
                        alertify.alert(ret.Msg);
                    }
                }).error(function (data, status, headers, config) {
                    $scope.DataLoading = false;
                    common.error($scope);
                    alertify.alert("请检查网络");
                })
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.DeleteBuilding = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../SafetyConfig/DeleteBuilding',
                    data: { 'Id': Id }
                }).success(function (ret) {
                    $scope.DataLoading = false;
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        alertify.alert(ret.Msg);
                        $scope.GetBuildingPageList(model, page);
                    } else {
                        alertify.alert(ret.Msg);
                    }
                }).error(function (data, status, headers, config) {
                    $scope.DataLoading = false;
                    common.error($scope);
                    alertify.alert("请检查网络");
                })
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.GetBuildingPageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.BuildingPageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuildingPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.BuildingPageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.AddBuilding = function (model) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/AddBuilding',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = "BuildingPageList?page=1";
                }, 2000);


            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.GetBuilding = function (Id) {
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuilding',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EditModel = ret.Data;
                    $scope.EditModel.ParkId = $scope.EditModel.ParkId + '';
                    $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetPark = function (Id) {
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetPark',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EditModel = ret.Data;
                    $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.EditBuilding = function (model) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/EditBuilding',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = "BuildingPageList?page=1";
                }, 2000);


            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.EditPark = function (model) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/EditPark',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = "ParkPageList?page=1";
                }, 2000);
            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.GetBuildingList = function (id) {
        $scope.BuildingList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuildingList',
            data: { 'ParkId': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.BuildingList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    //...............................楼层..................................................................

    $scope.GetBuildingFloorPageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.FloorPageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuildingFloorPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.FloorPageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.AddBuildingFloor = function (model) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/AddBuildingFloor',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = "BuildingFloorPageList?page=1";
                }, 2000);


            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.DeleteBuildingFloor = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../SafetyConfig/DeleteBuildingFloor',
                    data: { 'Id': Id }
                }).success(function (ret) {
                    $scope.DataLoading = false;
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        alertify.alert(ret.Msg);
                        $scope.GetBuildingFloorPageList(model, page);
                    } else {
                        alertify.alert(ret.Msg);
                    }
                }).error(function (data, status, headers, config) {
                    $scope.DataLoading = false;
                    common.error($scope);
                    alertify.alert("请检查网络");
                })
            }, function (ev) {
                ev.preventDefault();
            });
    }
    $scope.GetBuildingFloor = function (Id) {
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuildingFloor',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EditModel = ret.Data;
                    $scope.EditModel.BuildingId = $scope.EditModel.BuildingId + '';
                    $scope.EditModel.ParkId = $scope.EditModel.ParkId + '';
                    $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.GetBuildingList($scope.EditModel.ParkId);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.EditBuildingFloor = function (model) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/EditBuildingFloor',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = "BuildingFloorPageList?page=1";
                }, 2000);


            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    //--------------------------------------------------------------------------------------------------



    //..........................................房间.....................................................

    $scope.GetBuildingRoomPageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.RoomPageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuildingRoomPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.RoomPageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetFloorList = function (id) {
        $scope.FloorList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetFloorList',
            data: { 'BuildingId': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.FloorList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.AddBuildingRoom = function (model, pagename, page) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/AddBuildingRoom',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    if (pagename != null) {
                        location.href = pagename + "?page=" + page;
                    }
                    else {
                        location.href = "BuildingRoomPageList?page=1";
                    }

                }, 2000);


            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }
    $scope.DeleteBuildingRoom = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../SafetyConfig/DeleteBuildingRoom',
                    data: { 'Id': Id }
                }).success(function (ret) {
                    $scope.DataLoading = false;
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        alertify.alert(ret.Msg);
                        $scope.GetBuildingRoomPageList(model, page);
                    } else {
                        alertify.alert(ret.Msg);
                    }
                }).error(function (data, status, headers, config) {
                    $scope.DataLoading = false;
                    common.error($scope);
                    alertify.alert("请检查网络");
                })
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.GetBuildingRoom = function (Id) {
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuildingRoom',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EditModel = ret.Data;
                    $scope.EditModel.BuildingId = $scope.EditModel.BuildingId + '';
                    $scope.EditModel.ParkId = $scope.EditModel.ParkId + '';
                    $scope.EditModel.FloorId = $scope.EditModel.FloorId + '';
                    $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.GetBuildingList($scope.EditModel.ParkId);
                    $scope.GetFloorList($scope.EditModel.BuildingId);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.EditBuildingRoom = function (model) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/EditBuildingRoom',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = "BuildingRoomPageList?page=1";
                }, 2000);


            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }
    //----------------------------------------------------------------------------------------------------



    //---------------------------------------场所---------------------------------------------------------

    $scope.GetUserByDepartment = function (departmentId) {
        $scope.PageLoading = true;
        $scope.UserList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetUserByDepartment',
            data: { 'DepartmentId': departmentId }
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.UserList = ret.Data;
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.GetPlacePageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.PlacePageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetPlacePageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.PageLoading = false;
                if (ret.Code == 100) {
                    $scope.PlacePageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetPlaceTypeList = function () {
        $scope.PlaceTypeList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetPlaceTypeList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.PlaceTypeList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetRoomList = function (id) {
        $scope.RoomList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetRoomList',
            data: { 'FloorId': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.RoomList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.AddPlace = function (model, pagename, page) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/AddPlace',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = pagename + "?page=" + page;
                }, 1000);


            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.DeletePlace = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../SafetyConfig/DeletePlace',
                    data: { 'Id': Id }
                }).success(function (ret) {
                    $scope.DataLoading = false;
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        alertify.alert(ret.Msg);
                        $scope.GetPlacePageList(model, page);
                    } else {
                        alertify.alert(ret.Msg);
                    }
                }).error(function (data, status, headers, config) {
                    $scope.DataLoading = false;
                    common.error($scope);
                    alertify.alert("请检查网络");
                })
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.GetPlaceInfo = function (Id) {
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetPlaceInfo',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EditModel = ret.Data;
                    $scope.EditModel.BuildingInfo.Id = $scope.EditModel.BuildingInfo.Id + '';
                    $scope.EditModel.ParkInfo.Id = $scope.EditModel.ParkInfo.Id + '';
                    $scope.EditModel.FloorInfo.Id = $scope.EditModel.FloorInfo.Id + '';
                    $scope.EditModel.RoomId = $scope.EditModel.RoomId + "";
                    $scope.EditModel.PlaceTypeId = $scope.EditModel.PlaceTypeId + "";
                    $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.GetBuildingList($scope.EditModel.ParkInfo.Id);
                    $scope.GetFloorList($scope.EditModel.BuildingInfo.Id);
                    $scope.GetRoomList($scope.EditModel.FloorInfo.Id);

                    $scope.BindDepartmentList('Select_Department', $scope.EditModel.DepartmentId);
                    $scope.GetUserByDepartment($scope.EditModel.DepartmentId);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }


    $scope.EditPlace = function (model, pagename, page) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyConfig/EditPlace',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = pagename + "?page=" + page;
                }, 2000);


            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.BindDepartmentList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../HiddenTrouble/GetDepartmentList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (n.DepartmentName != "系统支持") {
                            if (defaultvalue != null) {
                                if (n.Id == defaultvalue)
                                    $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                                else
                                    $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                            else {
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    //...................................................................................................


    //------------------------------------------------------------------------------------------
    $scope.GetEmailTempleteList = function () {
        $scope.TempleteList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetEmailTempleteList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TempleteList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetEmailUserList = function () {
        $scope.EmailUserList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetEmailUserList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EmailUserList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetLeaderList = function () {
        $scope.LeaderList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetLeaderList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.LeaderList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetTempleteContent = function (id) {
        if (id == '') {
            $("#EmailContent").html("");
        }
        for (var i = 0; i < $scope.TempleteList.length; i++) {
            if ($scope.TempleteList[i].Id == id) {
                $("#EmailContent").html($scope.TempleteList[i].TemleteContent);
            }
        }
    }

    $scope.ChooseRec = function () {
        var controls = $("#ChooseReceiver [name='chk_rec']");
        $scope.UserArray = [];
        for (var i = 0; i < controls.length; i++) {
         
            if ($(controls[i]).prop("checked")) {
                var id = controls[i].id;
                var v = controls[i].value;
                var email = document.getElementById(id).getAttribute("u-data");
                var name = document.getElementById(id).getAttribute("u-name");
                var obj = {
                    "UserNo": id,
                    "UserType": v,
                    "SendType": 1,
                    "UserEmail": email,
                    "UserName": name,
                    "SortBy":i
                };
                $scope.UserArray.push(obj);
            }
        }
        $(".close").click();
    }
    $scope.ChooseCC = function () {
        var controls = $("#ChooseCC [name='chk_cc']");
        $scope.CCArray = [];
        for (var i = 0; i < controls.length; i++) {

            if ($(controls[i]).prop("checked")) {
                var id = controls[i].id;
                var v = controls[i].value;
                var email = document.getElementById(id).getAttribute("u-data");
                var name = document.getElementById(id).getAttribute("u-name");
                var obj = {
                    "UserNo": id,
                    "UserType": v,
                    "SendType": 2,
                    "UserEmail": email,
                    "UserName": name,
                    "SortBy": i
                };
                $scope.CCArray.push(obj);
            }
        }
        $(".close").click();
    }
    $scope.RemoveItems = function (list, x) {
        for (var i = 0; i < list.length; i++) {
            if (list[i].UserNo == x.UserNo) {
                list.splice(i, 1);
            }
        }
    }

    $scope.AddNoticeRule = function (model) {
        var rec_list = $("#SelectedCheckList li");
        for (var i = 0; i < rec_list.length; i++) {
            for (var j = 0; j < $scope.UserArray.length; j++) {
                if ("send_" + $scope.UserArray[j].UserNo == rec_list[i].id) {
                    $scope.UserArray[j].SortBy= i;
                }
            }
        }
        var cc_list = $("#SelectedCheckList2 li");
        for (var i = 0; i < rec_list.length; i++) {
            for (var j = 0; j < $scope.CCArray.length; j++) {
                if ("cc_" + $scope.CCArray[j].UserNo == rec_list[i].id) {
                    $scope.CCArray[j].SortBy = i;
                }
            }
        }
        if ($scope.UserArray.length == 0) {
            alertify.alert("请选择收件人");
            return false;
        }
        $http({
            method: 'POST',
            url: '../SafetyConfig/AddEmailNoticeRule',
            data: { 'model': model, 'ulist': $scope.UserArray, 'clist': $scope.CCArray }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = "EmailNoticeConfigList";
                }, 1000);
            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }


    $scope.GetEmailNoticePageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.RulePageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetEmailNoticePageList',
            data: { "SearchText": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.RulePageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.ChangeRuleStatus = function (Id, status,model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../SafetyConfig/ChangeNoticeRuleStatus',
                    data: { 'Id': Id, 'Status': status }
                }).success(function (ret) {
                    $scope.DataLoading = false;
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        alertify.alert(ret.Msg);
                        $scope.GetEmailNoticePageList(model, page);

                    } else {
                        alertify.alert(ret.Msg);
                    }
                }).error(function (data, status, headers, config) {
                    $scope.DataLoading = false;
                    common.error($scope);
                    alertify.alert("请检查网络");
                })
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.GetNoticeRuleDetail = function (Id) {
        $scope.PageLoading = true;
        setTimeout(function () {
            $scope.EditModel = {};
            $http({
                method: 'POST',
                url: '../SafetyConfig/GetNoticeRuleDetail',
                data: { 'Id': Id }
            })
                .success(function (ret) {
                    common.success($scope, null, ret);
                    $scope.PageLoading = false;
                    if (ret.Code == 100) {
                        $scope.EditModel = ret.Data;
                        $scope.EditModel.EmailTempleteId = $scope.EditModel.EmailTempleteId + "";
                        $scope.GetTempleteContent($scope.EditModel.EmailTempleteId);

                        for (var i = 0; i < $scope.EditModel.UserList.length; i++) {
                            var u = $scope.EditModel.UserList[i];

                            if (u.SendType === 1) {
                                var obj = {
                                    "UserNo": u.UserNo,
                                    "UserType": u.UserType,
                                    "SendType": 1,
                                    "UserEmail": u.UserEmail,
                                    "UserName": u.UserName,
                                    "SortBy": i
                                };
                                $scope.UserArray.push(obj);
                                if (u.UserType == 1) {
                                    $("#u_0").prop("checked", true);
                                }
                                else if (u.UserType == 2) {
                                    $("#u_1").prop("checked", true);
                                }
                                else {
                                    $("#ChooseReceiver").find("#" + u.UserNo).prop("checked", true);
                                }

                            }
                            else if (u.SendType === 2) {
                                console.log(u.SendType);
                                var obj = {
                                    "UserNo": u.UserNo,
                                    "UserType": u.UserType,
                                    "SendType": 2,
                                    "UserEmail": u.UserEmail,
                                    "UserName": u.UserName,
                                    "SortBy": i
                                };
                                $scope.CCArray.push(obj);
                                if (u.UserType === 1) {
                                    $("#c_0").prop("checked", true);
                                }
                                else if (u.UserType === 2) {
                                    $("#c_1").prop("checked", true);
                                }
                                else {
                                    $("#ChooseCC").find("#" + u.UserNo).prop("checked", true);
                                }

                            }
                        }
                    }
                    else {
                        alertify.alert(ret.Msg);
                    }
                })
                .error(function (data, status, headers, config) {
                    alertify.alert("请检查网络");
                });
        },2000);
    }


    $scope.EditEmailNotice = function (model,page) {
        var rec_list = $("#SelectedCheckList li");
        for (var i = 0; i < rec_list.length; i++) {
            for (var j = 0; j < $scope.UserArray.length; j++) {
                if ("send_" + $scope.UserArray[j].UserNo == rec_list[i].id) {
                    $scope.UserArray[j].SortBy = i;
                }
            }
        }
        var cc_list = $("#SelectedCheckList2 li");
        for (var i = 0; i < rec_list.length; i++) {
            for (var j = 0; j < $scope.CCArray.length; j++) {
                if ("cc_" + $scope.CCArray[j].UserNo == rec_list[i].id) {
                    $scope.CCArray[j].SortBy = i;
                }
            }
        }
        if ($scope.UserArray.length == 0) {
            alertify.alert("请选择收件人");
            return false;
        }
        $http({
            method: 'POST',
            url: '../SafetyConfig/EditEmailNoticeRule',
            data: { 'model': model, 'ulist': $scope.UserArray, 'clist': $scope.CCArray }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    location.href = "EmailNoticeConfigList?page=" + page;
                }, 1000);
            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.GetEmailNoticeLogPageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.LogPageList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetEmailNoticeLogPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.LogPageList = ret.Data.Data;
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

   
    //------------------------------------------------------------------------------------------------
    $scope.GetPlaceByBuilding = function (Id) {
        $scope.PageLoading = true;
        $scope.PlaceList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetPlaceListByBuilding',
            data: { "BuildingId": Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.PageLoading = false;
                if (ret.Code == 100) {
                    $scope.PlaceList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.AddParkMap = function (model, backurl) {
        var files = document.querySelector('input[type=file]').files;
        if (files.length == 0) {
            alertify.alert("请上传园区场景图！");
            return false;
        }
        Upload.upload({
            method: 'POST',
            url: '../SafetyConfig/AddPlaceMap',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("场景图添加成功！");
                    setTimeout(function () {
                        location.href = location.href;
                    }, 2000)
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }
    $scope.MGetParkMap = function (parkid) {
        mui.showLoading("正在加载..", "div");
        $scope.MapList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetParkMapList',
            data: { "ParkId": parkid },
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.MapList = ret.Data;
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.MGetBuildingMap = function (id) {
        mui.showLoading("正在加载..", "div");
        $scope.MapList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetBuildingMapList',
            data: { "BuildingId": id },
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.MapList = ret.Data;
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
});



app.filter('GetStatus', function () { //可以注入依赖
    return function (text) {
        if (text == '0')
            return "禁用";
        else if (text == '1')
            return "启用";

    }
});

app.filter('GetBoolStr', function () { //可以注入依赖
    return function (text) {
        if (text)
            return "启用";
        else
            return "禁用";

    }
});
app.filter('GetWeekDay', function () {
    return function (enddate) {
        var endDate = new Date(enddate);
        var beginDate = new Date(endDate.getFullYear(), 0, 1);
        //星期从0-6,0代表星期天，6代表星期六
        var endWeek = endDate.getDay();
        if (endWeek == 0) endWeek = 7;
        var beginWeek = beginDate.getDay();
        if (beginWeek == 0) beginWeek = 7;
        //计算两个日期的天数差
        var millisDiff = endDate.getTime() - beginDate.getTime();
        var dayDiff = Math.floor((millisDiff + (beginWeek - endWeek) * (24 * 60 * 60 * 1000)) / 86400000);
        return Math.ceil(dayDiff / 7) + 1;
    }

});